package com.example.demo.util;

/**
 * ImageTools
 *
 * @author Wizard
 * @date 2012-1-30
 */

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;

public class ImageTools {

    /**
     * 从(0,0)开始截取图片
     *
     * @param imgSrc  图片文件
     * @param imgDest 输出图片路径
     * @param width   宽
     * @param height  长
     * @return
     */
    public static Boolean cutting(String imgSrc, String imgDest, int width, int height) {
        File file = new File(imgSrc);
        return cutting(file, imgDest, 0, 0, width, height);
    }

    /**
     * 从(0,0)开始截取图片
     *
     * @param imgSrc  图片文件
     * @param imgDest 输出图片路径
     * @param width   宽
     * @param height  长
     * @return
     */
    public static Boolean cutting(String imgSrc, String imgDest, int x, int y, int width, int height) {
        File file = new File(imgSrc);
        return cutting(file, imgDest, x, y, width, height);
    }

    /**
     * 从(0,0)开始截取图片
     *
     * @param file    图片文件
     * @param imgDest 输出图片路径
     * @param width   宽
     * @param height  长
     * @return
     */
    public static Boolean cutting(File file, String imgDest, int width, int height) {
        return cutting(file, imgDest, 0, 0, width, height);
    }

    /**
     * 截取图片
     *
     * @param file    图片文件
     * @param imgDest 输出图片路径
     * @param x       横坐标
     * @param y       纵坐标
     * @param width   宽
     * @param height  长
     * @return
     */
    public static Boolean cutting(File file, String imgDest, int x, int y, int width, int height) {
        ImageOutputStream out = null;
        InputStream is = null;
        try {
            String endName = file.getName();
            endName = endName.substring(endName.lastIndexOf(".") + 1);
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(endName);
            ImageReader reader = (ImageReader) readers.next();
            is = new FileInputStream(file);
            ImageInputStream iis = ImageIO.createImageInputStream(is);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            File newFile = new File(imgDest);
            out = ImageIO.createImageOutputStream(new FileOutputStream(newFile));
            ImageIO.write(bi, endName, out);
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {
                }
            org.apache.tomcat.util.http.fileupload.IOUtils.closeQuietly(is);
        }
    }

    public static Boolean cutting(InputStream is, String fileName, String imgDest, int x, int y, int width, int height) {
        ImageOutputStream out = null;
        try {
            String endName = fileName.substring(fileName.lastIndexOf(".") + 1);
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(endName);
            ImageReader reader = (ImageReader) readers.next();
            ImageInputStream iis = ImageIO.createImageInputStream(is);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            File newFile = new File(imgDest);
            out = ImageIO.createImageOutputStream(new FileOutputStream(newFile));
            ImageIO.write(bi, endName, out);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {
                }
            org.apache.tomcat.util.http.fileupload.IOUtils.closeQuietly(is);
        }
    }


    public static void zooomAndcut(InputStream is, String firmImageFileName, String destPath, Integer zoomHeight, Integer zoomWidth, Integer x, Integer y, Integer height, Integer width) throws Exception {
        // 缩放原图片
        ByteArrayOutputStream bs = null;
        ImageOutputStream imOut = null;
        InputStream dis = null;
        try {
            BufferedImage destImage = new BufferedImage(zoomWidth, zoomHeight,
                    BufferedImage.TYPE_INT_RGB);
            destImage.getGraphics().drawImage(ImageIO.read(is), 0, 0, zoomWidth,
                    zoomHeight, null);
            //bufferedImage转inputstream
            destImage.flush();
            bs = new ByteArrayOutputStream();
            imOut = ImageIO.createImageOutputStream(bs);
            String endName = firmImageFileName.substring(firmImageFileName.lastIndexOf(".") + 1);
            ImageIO.write(destImage, endName, imOut);
            dis = new ByteArrayInputStream(bs.toByteArray());

            //裁剪缩放后的图片
            ImageTools.cutting(dis, firmImageFileName, destPath, x, y, width, height);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                if (dis != null)
                    dis.close();
                if (bs != null)
                    bs.close();
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
        }
    }

    public static String getPicSuffix(String img_path) {
        if (img_path == null || img_path.indexOf(".") == -1) {
            return ""; //如果图片地址为null或者地址中没有"."就返回""
        }
        return img_path.substring(img_path.lastIndexOf(".") + 1).
                trim().toLowerCase();
    }
}
